<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="${packagePath}.dao.${table.className}Mapper">

<resultMap id="BaseResultMap" type="${packagePath}.model.${table.className}">
<#list table.columns as column>
	 <#if column.dataType=="String">
	 <result property="${column.fieldName}" column="${column.columnName}" jdbcType="VARCHAR" javaType="java.lang.String"/>
	 <#elseif column.dataType=="Long">
	 <result property="${column.fieldName}" column="${column.columnName}" jdbcType="NUMERIC" javaType="java.lang.Long"/>
	 <#elseif column.dataType=="Integer">
	 <result property="${column.fieldName}" column="${column.columnName}" jdbcType="NUMERIC" javaType="java.lang.Integer"/>
	 <#elseif column.dataType=="BigDecimal">
	 <result property="${column.fieldName}" column="${column.columnName}" jdbcType="DECIMAL" javaType="java.math.BigDecimal"/>
	 <#else>
	 <!-- 该字段不是String,Long,Date类型，请自行修改对应的类型 -->
	 <result property="${column.fieldName}" column="${column.columnName}" jdbcType="VARCHAR" javaType="java.lang.String"/>
	 </#if>
</#list>
</resultMap>

<sql id="BaseField">
    <#list table.columns as column>
    	t.${column.columnName}<#t>
    	<#if column_has_next>,<#t></#if>
    </#list>
</sql>

<sql id="BaseSelective">
	select <include refid="BaseField" /> from ${tableName} t
</sql>

<sql id="BaseConditions">
   <where>
		<#list table.columns as column>
			<if test="${column.fieldName} != null and ${column.fieldName} != ''">
				and t.${column.columnName} = ${"#"}${"{"}${column.fieldName}${"}"}
	        </if>
		</#list>
	</where>
</sql>

<insert id="save" parameterType="${packagePath}.model.${table.className}">
	insert into ${tableName}
	<trim prefix="(" suffixOverrides="," suffix=")">        
<#list table.columns as column>
		<if test="${column.fieldName} != null and ${column.fieldName} != ''">
			${column.columnName},
		</if>
</#list>
	</trim>
		values
	<trim prefix="(" suffixOverrides="," suffix=")">	    
<#list table.columns as column>
		<if test="${column.fieldName} != null and ${column.fieldName} != ''">
			${"#"}${"{"}${column.fieldName}${"}"},
    	</if>
</#list>
	</trim>
</insert>

<update id="update" parameterType="${packagePath}.model.${table.className}">
	update ${tableName}
	<trim prefix="set" suffixOverrides=",">
	<#list table.columns as column>
		<if test="${column.fieldName} != null and ${column.fieldName} != ''">
			${column.columnName} = ${"#"}${"{"}${column.fieldName}${"}"},
		</if>
	</#list>
	</trim>
<#list table.columns as column>
<#if column.columnKey == 'PRI'>
	where ${column.columnName} = ${"#"}${"{"}${column.fieldName}${"}"}
</#if>
</#list>
</update>

<delete id="delete" parameterType="java.lang.String">
    delete from ${tableName}
<#list table.columns as column>
<#if column.columnKey == 'PRI'>
	where ${column.columnName} = ${"#"}${"{id}"}
</#if>
</#list>
</delete>

<select id="find" parameterType="java.lang.String"  resultMap="BaseResultMap">
	<include refid="BaseSelective" />
<#list table.columns as column>
<#if column.columnKey == 'PRI'>
	where ${column.columnName} = ${"#"}${"{id}"}
</#if>
</#list>
</select>

<select id="findList" parameterType="${packagePath}.model.${table.className}"  resultMap="BaseResultMap">
	<include refid="BaseSelective" />
	<include refid="BaseConditions" />
</select>

</mapper>